
/*
'===============================================================================
'  HNG.Portal.BL.TblDanhMucTinManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using HNG.Portal.DAL.DatabaseSpecific;
using System.Text;

namespace HNG.Portal.BLL
{
	public class TblDanhMucTinManager : TblDanhMucTinManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblDanhMucTinManager()
		{
			// Nothing for now.
		}

        public DataTable Get_Bykeysearch(string keysearch, string columnName,int LangID)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            string strQuery = "select * from TblDanhMucTin ";
            if (columnName != "")
                strQuery += " where " + columnName + " like N'%" + keysearch + "%'" + " and LangID= " + LangID + " Order by ID desc";
            else
                strQuery += " where TenDanhMuc like N'%" + keysearch + "%'" + " and LangID= " + LangID + " Order by ID desc";
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, strQuery);
        }


        public DataTable Admin_Select_ByNotDmcha(int _DMcha, int LangID)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            string strQuery = "select * from TblDanhMucTin ";
            strQuery += " where DanhMucCha <> " + _DMcha + " and LangID= " + LangID + " Order by ID desc";
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, strQuery);
        }


        public DataTable SelectCommon(TblDanhMucTinEntity _TblDanhMucTinEntity)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            SqlParameter[] _SqlParameter = new SqlParameter[6]; 
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblDanhMucTinEntity.SortExpression) ? "[ID]" : _TblDanhMucTinEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblDanhMucTinEntity.SortDirection) ? " DESC" : _TblDanhMucTinEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[ID],").Append("\n");
                sbSql.Append("  ch.[LangID],").Append("\n");
                sbSql.Append("  ch.[DanhMucCha],").Append("\n");
                sbSql.Append("  ch.[TenDanhMucCha],").Append("\n");
                sbSql.Append("  ch.[TenDanhMuc],").Append("\n");
                sbSql.Append("  ch.[ThuTu],").Append("\n");
                sbSql.Append("  ch.[ViTri],").Append("\n");
                sbSql.Append("  ch.[MoTa],").Append("\n");
                sbSql.Append("  ch.[Url],").Append("\n");
                sbSql.Append("  ch.[Icon],").Append("\n");
                sbSql.Append("  ch.[Type],").Append("\n");
                sbSql.Append("  ch.[IsHome],").Append("\n");
                sbSql.Append("  ch.[TrangThai],").Append("\n");
                sbSql.Append("  lng.[Name] LangName").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblDanhMucTin ch").Append("\n");
                sbSql.AppendFormat("  LEFT JOIN tblLang lng").Append("\n");
                sbSql.Append("  ON lng.ID = ch.LangID").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.ID))
                {
                    _SqlParameter[0] = new SqlParameter("@ID", _TblDanhMucTinEntity.Id);
                    sbSql.Append("  AND ch.[ID] LIKE @ID").Append("\n");
                }

                string strCon = _TblDanhMucTinEntity.Condition;
                if (!string.IsNullOrEmpty(strCon))
                {
                    sbSql.AppendFormat("  AND (ch.[ID] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[LangID] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DanhMucCha] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenDanhMuc] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Url] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Icon] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Type] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[IsHome] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TrangThai] LIKE '%{0}%')", strCon).Append("\n");
                    string strUTF = _TblDanhMucTinEntity.ConditionUTF;
                     if (!string.IsNullOrEmpty(strUTF))
                     {
                         sbSql.AppendFormat("  OR (ch.[ID] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[LangID] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[DanhMucCha] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[TenDanhMuc] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[MoTa] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[Url] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[Icon] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[Type] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[IsHome] LIKE N'%{0}%'", strUTF).Append("\n");
                         sbSql.AppendFormat("  OR ch.[TrangThai] LIKE N'%{0}%')", strUTF).Append("\n");
                     }
                }

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.LangId))
                {
                    _SqlParameter[1] = new SqlParameter("@LangId", _TblDanhMucTinEntity.LangId);
                    sbSql.Append("  AND ch.[LangId] Like @LangId").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.DM))
                {
                    _SqlParameter[2] = new SqlParameter("@DanhMucCha", _TblDanhMucTinEntity.DM);
                    sbSql.Append("  AND ch.[DanhMucCha] Like @DanhMucCha").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.Type))
                {
                    _SqlParameter[3] = new SqlParameter("@Type", _TblDanhMucTinEntity.Type);
                    sbSql.Append("  AND ch.[type] Like @Type").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.strIsHome))
                {
                    _SqlParameter[4] = new SqlParameter("@IsHome", _TblDanhMucTinEntity.IsHome);
                    sbSql.Append("  AND ch.[IsHome] Like @IsHome").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.Status))
                {
                    _SqlParameter[5] = new SqlParameter("@TrangThai", _TblDanhMucTinEntity.TrangThai);
                    sbSql.Append("  AND ch.[TrangThai] Like @TrangThai").Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblDanhMucTinEntity.StartIndex, _TblDanhMucTinEntity.EndIndex).Append("\n");
            }
            catch { }
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString(), _SqlParameter);
        }

        public DataTable SelectDMTinPK(TblDanhMucTinEntity _TblDanhMucTinEntity)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblDanhMucTinEntity.SortExpression) ? "[ID]" : _TblDanhMucTinEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblDanhMucTinEntity.SortDirection) ? " DESC" : _TblDanhMucTinEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[ID],").Append("\n");
                sbSql.Append("  ch.[LangID],").Append("\n");
                sbSql.Append("  ch.[DanhMucCha],").Append("\n");
                sbSql.Append("  ch.[TenDanhMucCha],").Append("\n");
                sbSql.Append("  ch.[TenDanhMuc],").Append("\n");
                sbSql.Append("  ch.[ThuTu],").Append("\n");
                sbSql.Append("  ch.[ViTri],").Append("\n");
                sbSql.Append("  ch.[MoTa],").Append("\n");
                sbSql.Append("  ch.[Url],").Append("\n");
                sbSql.Append("  ch.[Icon],").Append("\n");
                sbSql.Append("  ch.[Type],").Append("\n");
                sbSql.Append("  ch.[IsHome],").Append("\n");
                sbSql.Append("  ch.[TrangThai],").Append("\n");
                sbSql.Append("  lng.[Name] LangName").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblDanhMucTin ch").Append("\n");
                sbSql.AppendFormat("  LEFT JOIN tblLang lng").Append("\n");
                sbSql.Append("  ON lng.ID = ch.LangID").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.ID))
                {
                    sbSql.AppendFormat("  AND ch.[ID] LIKE {0}", _TblDanhMucTinEntity.Id).Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblDanhMucTinEntity.StartIndex, _TblDanhMucTinEntity.EndIndex).Append("\n");
            }
            catch { }
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString());
        }

        public DataTable be_SelectToDataList(TblDanhMucTinEntity _TblDanhMucTinEntity)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            StringBuilder sbSql = new StringBuilder();
            SqlParameter[] _SqlParameter = new SqlParameter[2];
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblDanhMucTinEntity.SortExpression) ? "[ID]" : _TblDanhMucTinEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblDanhMucTinEntity.SortDirection) ? " DESC" : _TblDanhMucTinEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[ID],").Append("\n");
                sbSql.Append("  ch.[TenDanhMucCha],").Append("\n");
                sbSql.Append("  ch.[TenDanhMuc],").Append("\n");
                sbSql.Append("  ch.[ThuTu],").Append("\n");
                sbSql.Append("  ch.[Url],").Append("\n");
                sbSql.Append("  ch.[Icon],").Append("\n");
                sbSql.Append("  ch.[ViTri],").Append("\n");
                sbSql.Append("  ch.[IsHome],").Append("\n");
                sbSql.Append("  ch.[TrangThai]").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblDanhMucTin ch").Append("\n");
                sbSql.AppendFormat("  LEFT JOIN tblLang lng").Append("\n");
                sbSql.Append("  ON lng.ID = ch.LangID").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.ID))
                {
                    _SqlParameter[0] = new SqlParameter("@ID", _TblDanhMucTinEntity.Id);
                    sbSql.Append("  AND ch.[ID] LIKE @ID").Append("\n");
                }
                string strCon = _TblDanhMucTinEntity.Condition;
                if (!string.IsNullOrEmpty(strCon))
                {
                    sbSql.AppendFormat("  AND (ch.[ID] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[LangID] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenDanhMucCha] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenDanhMuc] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Url] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Icon] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Type] LIKE '%{0}%'", strCon).Append("\n");

                    sbSql.AppendFormat("  OR ch.[ID] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[LangID] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenDanhMucCha] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenDanhMuc] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Url] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Icon] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Type] LIKE N'%{0}%'", strCon).Append("\n");

                    sbSql.Append(")").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.LangId))
                {
                    _SqlParameter[1] = new SqlParameter("@LangId", _TblDanhMucTinEntity.LangId);
                    sbSql.Append("  AND ch.[LangId] Like @LangId").Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblDanhMucTinEntity.StartIndex, _TblDanhMucTinEntity.EndIndex).Append("\n");
            }
            catch { }


            return AE.SqlHelper.ExecuteDataTable(Conn,CommandType.Text,sbSql.ToString(),_SqlParameter);
        }

        public DataTable be_SelectToDropdownlist(TblDanhMucTinEntity _TblDanhMucTinEntity)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            StringBuilder sbSql = new StringBuilder();
            SqlParameter[] _SqlParameter = new SqlParameter[2];
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblDanhMucTinEntity.SortExpression) ? "[ID]" : _TblDanhMucTinEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblDanhMucTinEntity.SortDirection) ? " DESC" : _TblDanhMucTinEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[ID],").Append("\n");
                sbSql.Append("  ch.[DanhMucCha],").Append("\n");
                sbSql.Append("  ch.[TenDanhMuc]").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblDanhMucTin ch").Append("\n");
                sbSql.AppendFormat("  LEFT JOIN tblLang lng").Append("\n");
                sbSql.Append("  ON lng.ID = ch.LangID").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.ID))
                {
                    _SqlParameter[0] = new SqlParameter("@ID", _TblDanhMucTinEntity.Id);
                    sbSql.Append("  AND ch.[ID] LIKE @ID").Append("\n");
                }
                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.LangId))
                {
                    _SqlParameter[1] = new SqlParameter("@LangId", _TblDanhMucTinEntity.LangId);
                    sbSql.Append("  AND ch.[LangId] Like @LangId").Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblDanhMucTinEntity.StartIndex, _TblDanhMucTinEntity.EndIndex).Append("\n");
            }
            catch { }


            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString(), _SqlParameter);
        }

        public int be_UpdateOrderUp(int CateID)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            SqlParameter[] _SqlParameter = new SqlParameter[1];
            _SqlParameter[0] = new SqlParameter("@ID", CateID);
            return AE.SqlHelper.ExecuteNonQuery(Conn, "sp_CateNewsOrdersUp", _SqlParameter);
        }

        public int be_UpdateOrderDown(int CateID)
        {
            SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            SqlParameter[] _SqlParameter = new SqlParameter[1];
            _SqlParameter[0] = new SqlParameter("@ID", CateID);
            return AE.SqlHelper.ExecuteNonQuery(Conn, "sp_CateNewsOrdersDown", _SqlParameter);
        }
	}
}
